const mongoose = require('mongoose')
const Schema = mongoose.Schema
// 回复表
const replySchema = new Schema({
  commentId:  {
    type: Schema.Types.ObjectId,
    ref: 'comment'
  }, // 评论ID
  content: String, // 内容
  type: {
    type: Number,
    default: 1
  },
  state: {
    type: Number,
    default: 1 // 1-审核通过 2-未通过
  },
  fromUser: {
    type: Schema.Types.ObjectId,
    ref: 'user'
  },
  toUser: {
    type: Schema.Types.ObjectId,
    ref: 'user'
  },
  meta: {
    createdAt: {
      type: Date,
      default: Date.now()
    },
    updatedAt: {
      type: Date,
      default: Date.now()
    }
  }
})

replySchema.pre('save', function (next) {
  if (this.isNew) {
    this.meta.createdAt = this.meta.updatedAt = Date.now()
  } else {
    this.meta.updatedAt = Date.now()
  }
  next()
})

mongoose.model('reply', replySchema)
